const cloud = require('wx-server-sdk')

cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV
})

const db = cloud.database()

// 测试数据
const testData = {
  nutritionists: [
    {
      _id: 'test-nutritionist-001',
      name: '王营养师',
      avatar: '/images/nutritionist/avatar.jpg',
      title: '高级营养师',
      specialties: ['减重营养', '运动营养', '孕产营养'],
      certification: ['国家高级营养师证书', '体育营养师证书'],
      experience: 5,
      introduction: '专注于健康饮食和体重管理，帮助客户实现健康目标。',
      rating: 4.8,
      reviewCount: 128,
      consultationCount: 256,
      isActive: true,
      createTime: db.serverDate(),
      updateTime: db.serverDate()
    }
  ],
  
  ingredients: [
    {
      _id: 'test-ingredient-001',
      name: '生菜',
      image: '/images/ingredients/lettuce.jpg',
      category: '蔬菜',
      nutrition: {
        calories: 15,
        protein: 1,
        carbs: 2.9,
        fat: 0.2,
        fiber: 1.3,
        vitamins: ['维生素A', '维生素C', '维生素K'],
        minerals: ['钾', '钙']
      },
      unit: '克',
      price: 0.02,
      stock: 1000,
      description: '新鲜脆嫩的生菜，富含维生素和膳食纤维。',
      tips: '建议生食，保持最佳营养价值。',
      isActive: true,
      createTime: db.serverDate(),
      updateTime: db.serverDate()
    },
    {
      _id: 'test-ingredient-002',
      name: '鸡胸肉',
      image: '/images/ingredients/chicken-breast.jpg',
      category: '肉类',
      nutrition: {
        calories: 165,
        protein: 31,
        carbs: 0,
        fat: 3.6,
        fiber: 0,
        vitamins: ['维生素B6', '维生素B12'],
        minerals: ['锌', '硒']
      },
      unit: '克',
      price: 0.04,
      stock: 500,
      description: '去皮鸡胸肉，优质蛋白质来源。',
      tips: '建议水煮或煎制，避免油炸。',
      isActive: true,
      createTime: db.serverDate(),
      updateTime: db.serverDate()
    }
  ],
  
  meals: [
    {
      _id: 'test-meal-001',
      name: '健康轻食沙拉套餐',
      description: '低脂高蛋白，营养均衡的健康轻食套餐',
      image: '/images/meals/sample-meal.jpg',
      price: 48,
      calories: 450,
      protein: 25,
      carbs: 45,
      fat: 15,
      ingredientIds: ['test-ingredient-001', 'test-ingredient-002'],
      nutritionistId: 'test-nutritionist-001',
      steps: [
        {
          title: '食材准备',
          image: '/images/steps/prep.jpg',
          description: '将生菜洗净切碎，鸡胸肉切丁，藜麦煮熟备用',
          tips: '鸡胸肉最好选择新鲜的，避免冷冻品'
        },
        {
          title: '调制酱汁',
          image: '/images/steps/sauce.jpg',
          description: '将橄榄油、柠檬汁、盐和黑胡椒混合均匀',
          tips: '可以根据个人口味调整酱汁酸度'
        }
      ],
      tags: ['低脂', '高蛋白', '轻食'],
      category: '沙拉',
      isActive: true,
      createTime: db.serverDate(),
      updateTime: db.serverDate()
    }
  ]
}

// 初始化数据
exports.main = async (event, context) => {
  try {
    // 清空现有数据
    await Promise.all([
      db.collection('nutritionists').where({}).remove(),
      db.collection('ingredients').where({}).remove(),
      db.collection('meals').where({}).remove()
    ])

    // 插入测试数据
    await Promise.all([
      ...testData.nutritionists.map(item => 
        db.collection('nutritionists').add({ data: item })
      ),
      ...testData.ingredients.map(item => 
        db.collection('ingredients').add({ data: item })
      ),
      ...testData.meals.map(item => 
        db.collection('meals').add({ data: item })
      )
    ])

    return {
      success: true,
      message: '测试数据初始化成功'
    }

  } catch (error) {
    console.error('初始化测试数据失败:', error)
    return {
      success: false,
      errMsg: error.message || '初始化测试数据失败'
    }
  }
} 